package com.example.demo.mapper;


import com.example.demo.enity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;


//@Mapper是放在接口上的
//他的作用（底层自动运作）：@Mapper == UserMapper userMapper=new (接口的实现类  （实现类需要重写接口中的方法）)
//再将对象放入ioc容器中 UserMapper userMapper=new 实现类（）
//调用时是根据父类的引用名进行调用  UserMapper userMapper


//放在接口上
@Repository
@Mapper
//关于mapper注解的解析：
//在底层自动生成一个类用于实现被Mapper注解修饰过的接口
//在拿到这个实现类的对象时用到了多态（父类引用指向子类对象的形式）
//@Mapper == UserMapper userMapper=new (接口的实现类  （实现类需要重写接口中的方法）)
//再将这个对象扫描到ioc容器中
public interface UserMapper {

    @Select("select id, username, password\n" +
            "from user_table where username=#{username}")
    public User getUserByUsername(String username);

    public List<User> selectUserList();

    @Select("select u.id, u.username, a.type\n" +
            "from user_table u,\n" +
            "     user_authority_table ua,\n" +
            "     authority_table a\n" +
            "where u.id = ua.u_id\n" +
            "  and a.id = ua.a_id\n" +
            "  and u.username = #{username}")
    public User selectUserByUsername(String username);

    public int insertUser(User user);

    @Select("select u.id, u.username, a.type\n" +
            "from user_table u,\n" +
            "     user_authority_table ua,\n" +
            "     authority_table a\n" +
            "where u.id = ua.u_id\n" +
            "  and a.id = ua.a_id\n" +
            "  and u.id=#{id}")
    public User selectUserById(Integer id);

    public int updateUser(User user);

    @Delete("delete\n" +
            "from user_table\n" +
            "where id = #{id}")
    public int deleteUserById(Integer id);

    @Update("update user_table set password=#{password} where id=#{id}\n")
    public int updateUserPassWordByUser(String password, Integer id);


}
